package com.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author: Ren(任佳宝)
 * @Description: CorsConfiguration
 * @Date: 2025/1/6 上午10:36
 * @Version: 1.0
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter() {

        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();

        // 配置跨区
        config.addAllowedOriginPattern("*"); // 请求源
        config.addAllowedHeader("*"); // 请求头
        config.addAllowedMethod("*"); // 请求方式
        config.setAllowCredentials(true); // cookie

        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

